using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CentralOffice.Admin.BusinessActions;
using DepartmentManager = CentralOffice.Admin.BusinessActions.Department;
using DepartmentTable = CentralOffice.Admin.Data.Departments.DepartmentsDataTable;
using DepartmentRow = CentralOffice.Admin.Data.Departments.DepartmentsRow;
using EmployeeTable = CentralOffice.Admin.Data.EmployeeDataSet.EmployeesDataTable;
using EmployeeRow = CentralOffice.Admin.Data.EmployeeDataSet.EmployeesRow;
using Husb.Util;

namespace CentralOffice.WebClient.HumanResources
{
    public class HumanResourcesHelper
    {
        public static void PopulateEmployeeTree(TreeView tvEmp)
        {
            PopulateEmployeeTree(tvEmp, false, true, null);
        }

        public static void PopulateEmployeeTree(TreeView tvEmp, string[] es)
        {
            PopulateEmployeeTree(tvEmp, true, false, es);
        }

        public static void PopulateEmployeeTree(TreeView tvEmp, bool showCheckBox, string[] es)
        {
            PopulateEmployeeTree(tvEmp, showCheckBox, false, es);
        }

        public static void PopulateEmployeeTree(TreeView tvEmp, bool showCheckBox, bool hasHyperLink, string[] es)
        {
            DepartmentTable depts = DepartmentManager.GetTopDepartment();
            EmployeeTable employees = Employee.GetAll();

            foreach (DepartmentRow row in depts.Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = "[" + row.Name + "]";
                node.Value = row.Id.ToString();
                //node.ToolTip = "e";
                node.SelectAction = TreeNodeSelectAction.Expand;
                //node.ImageUrl = "../../images/menu/system.gif";
                node.ImageUrl = "~/images/menu/system.gif";
                //node.ToolTip = row.DepartmentNumber;
                //node.PopulateOnDemand = true;
                //node.SelectAction = TreeNodeSelectAction.Select;

                node.ShowCheckBox = showCheckBox;

                tvEmp.Nodes.Add(node);
                node.Expanded = true;

                PopulateEmployee(node, employees, showCheckBox, hasHyperLink, es);

                AddDepartment(node, employees, showCheckBox, hasHyperLink, es);
            }
        }

        private static void AddDepartment(TreeNode node, EmployeeTable employees, bool showCheckBox, bool hasHyperLink,  string[] es)
        {
            Guid id = new Guid(node.Value);
            DepartmentTable depts = DepartmentManager.GetSubDepartment(id);

            foreach (DepartmentRow dept in depts)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = "[" + dept.Name + "]"; //dept.Name;
                newNode.Value = dept.Id.ToString();
                newNode.Target = "hrms";
                //newNode.ToolTip = "d";
                //newNode.ImageUrl = "../../images/node_dept.gif";
                newNode.ImageUrl = "~/images/node_dept.gif";
                if (hasHyperLink)
                {
                    newNode.NavigateUrl = "~/HumanResources/Manage/DepartmentEmployees.aspx?deptid=" + dept.Id.ToString() + "&parentid=" + node.Value;
                }
                else
                {
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                }
                //node.ToolTip = newNode.DepartmentNumber;
                //newNode.SelectAction = TreeNodeSelectAction.Expand;
                newNode.ShowCheckBox = showCheckBox;
                

                node.ChildNodes.Add(newNode);
                newNode.Expanded = newNode.Depth < 2;

                PopulateEmployee(newNode, employees, showCheckBox, hasHyperLink, es);

                AddDepartment(newNode, employees, showCheckBox, hasHyperLink, es);
            }
        }

        private static void PopulateEmployee(TreeNode node, EmployeeTable employees, bool showCheckBox, bool hasHyperLink,  string[] es)
        {
            EmployeeTable emps = Employee.GetEmployeesByDepartmentId(employees, new Guid(node.Value));
            foreach (EmployeeRow row in emps)
            {
                TreeNode employeeNode = new TreeNode();
                employeeNode.Text = row.Name;
                employeeNode.Value = row.Id.ToString();
                employeeNode.Target = "hrms";
                //employeeNode.ToolTip = "e";
                if (hasHyperLink)
                {
                    employeeNode.NavigateUrl = "~/HumanResources/Manage/EmployeeUpdate.aspx?empid=" + employeeNode.Value + "&parentid=" + node.Value;
                }
                else
                {
                    employeeNode.SelectAction = TreeNodeSelectAction.None;
                }
                employeeNode.ImageUrl = row.Gender == "Ů" ? "~/images/1-1.gif" : "~/images/0-1.gif";
                

                if (showCheckBox)
                {
                    employeeNode.ShowCheckBox = true;
                    if (CommonUtil.Contains(es, employeeNode.Text))
                    {
                        employeeNode.Checked = true;
                    }
                }

                node.ChildNodes.Add(employeeNode);
            }
        }
    }
}
